<?php
/**
 * Created by PhpStorm.
 * User: 17208
 * Date: 2019/6/26
 * Time: 16:18
 */
namespace app\api\timer;

use app\common\lib\Redis;
use app\common\facade\WebSocketToken;
use app\common\lib\Export;
use think\Exception;
use think\facade\Log;
class Matching extends Base
{
    private $lock = "Matching_lock";
    private $time = 2;
    public function __construct()
    {
        parent::__construct($this->lock, $this->time);
    }

    public function run(){
        try {
            // 获取锁
            $this->get_lock();

            $matching_people = Redis::get(config("REDIS_MATCHING"));
            // 无匹配数据
            if(!$matching_people){
                // 释放锁
                $this->unset_lock();
                return ;
            }
            while (count($matching_people)>1){
                $matching_people_bak = $matching_people;
                $user_id = array_shift($matching_people);
                $matching_people_id = $this->match($user_id,$matching_people);

                // 发送消息
                $user_fd = WebSocketToken::get_fd($user_id);
                $matching_people_fd = WebSocketToken::get_fd($matching_people_id);


                $all_exist = [];
                if(WebSocketToken::exist($user_fd) && WebSocketToken::exist($matching_people_fd)){
                    Export::socket_exp_ok($user_fd,Export::$socket_match_cmd,[ "matching_people_id" => $matching_people_id]);
                    Export::socket_exp_ok($matching_people_fd,Export::$socket_match_cmd,["matching_people_id" => $user_id]);
                    $all_exist = [
                        $user_id,$matching_people_id
                    ];
                }

                // 人员更新
                foreach ($matching_people_bak as $key=>$value){
                    if($value == $matching_people_id && !WebSocketToken::exist($matching_people_fd)){
                        unset($matching_people_bak[$key]);
                    }

                    if($value == $user_id && !WebSocketToken::exist($user_fd)){
                        unset($matching_people_bak[$key]);
                    }
                    if(in_array($value,$all_exist)){
                        unset($matching_people_bak[$key]);
                    }
                }
                $matching_people = $matching_people_bak;
            }
            Redis::set(config("REDIS_MATCHING"),$matching_people);

            // 释放锁
            $this->unset_lock();
        } catch (Exception $e) {
            Log::info("在线匹配异常：".$e->getMessage());
        }
    }
    private function match($user,$matching_people){
        $matching_people_id = 0;
        $compatibility = 0;
        foreach ($matching_people as $person){
            $the_compatibility = $this->get_compatibility($user,$person);
            if($the_compatibility>$compatibility){
                $matching_people_id = $person;
            }
        }
        return $matching_people_id;
    }
    private function get_compatibility($user,$person){
        return 1;
    }
}